package com.aplink.generic.xmlparser.ctrlformat;

import org.w3c.dom.Node;

import android.text.TextUtils;

import com.aplink.generic.constant.type.GoogleMapsType;
import com.aplink.generic.pojo.controlspec.GoogleMapsSpec;
import com.aplink.generic.xmlparser.XMLPaserUtil;

public class GoogleMapsFormat {
    public static final String CAMERA_BEARING = "cameraBearing";
    public static final String CAMERA_TARGET_LAT = "cameraTargetLat";
    public static final String CAMERA_TARGET_LNG = "cameraTargetLng";
    public static final String CAMERA_TILT = "cameraTilt";
    public static final String CAMERA_ZOOM = "cameraZoom";
    public static final String MAP_TYPE = "mapType";
    public static final String SET_GPS_LOCATION = "isSetGPSLocation";
    public static final String UI_COMPASS = "uiCompass";
    public static final String UI_ROTATE_GESTURES = "uiRotateGestures";
    public static final String UI_SCROLL_GESTURES = "uiScrollGestures";
    public static final String UI_ZOOM_CONTROLS = "uiZoomControls";
    public static final String UI_ZOOM_GESTURES = "uiZoomGestures";

    public static GoogleMapsSpec parse(final Node node) {
        final GoogleMapsSpec googleMapsSpec = new GoogleMapsSpec();
        if (node.hasAttributes()) {
            String value = XMLPaserUtil.getAttrValue(node,
                    GoogleMapsFormat.MAP_TYPE);
            if (!TextUtils.isEmpty(value)) {
                googleMapsSpec.setGoogleMapsType(GoogleMapsType.get(value));
            }

            value = XMLPaserUtil
                    .getAttrValue(node, GoogleMapsFormat.UI_COMPASS);
            if (!TextUtils.isEmpty(value)) {
                if (value.equalsIgnoreCase("N")) {
                    googleMapsSpec.setUiCompass(false);
                }
            } else {
                googleMapsSpec.setUiCompass(true);
            }

            value = XMLPaserUtil.getAttrValue(node,
                    GoogleMapsFormat.UI_ROTATE_GESTURES);
            if (!TextUtils.isEmpty(value)) {
                if (value.equalsIgnoreCase("N")) {
                    googleMapsSpec.setUiRotateGestures(false);
                }
            } else {
                googleMapsSpec.setUiRotateGestures(true);
            }

            value = XMLPaserUtil.getAttrValue(node,
                    GoogleMapsFormat.UI_SCROLL_GESTURES);
            if (!TextUtils.isEmpty(value)) {
                if (value.equalsIgnoreCase("N")) {
                    googleMapsSpec.setUiScrollGestures(false);
                }
            } else {
                googleMapsSpec.setUiScrollGestures(true);
            }

            value = XMLPaserUtil.getAttrValue(node,
                    GoogleMapsFormat.UI_ZOOM_GESTURES);
            if (!TextUtils.isEmpty(value)) {
                if (value.equalsIgnoreCase("N")) {
                    googleMapsSpec.setUiZoomGestures(false);
                }
            } else {
                googleMapsSpec.setUiZoomGestures(true);
            }

            value = XMLPaserUtil.getAttrValue(node,
                    GoogleMapsFormat.UI_ZOOM_CONTROLS);
            if (!TextUtils.isEmpty(value)) {
                if (value.equalsIgnoreCase("N")) {
                    googleMapsSpec.setUiZoomControls(false);
                }
            } else {
                googleMapsSpec.setUiZoomControls(true);
            }

            value = XMLPaserUtil.getAttrValue(node,
                    GoogleMapsFormat.CAMERA_ZOOM);
            if (!TextUtils.isEmpty(value)) {
                googleMapsSpec.setCameraZoom(Double.parseDouble(value));
            }

            value = XMLPaserUtil.getAttrValue(node,
                    GoogleMapsFormat.CAMERA_TILT);
            if (!TextUtils.isEmpty(value)) {
                googleMapsSpec.setCameraTilt(Double.parseDouble(value));
            }

            value = XMLPaserUtil.getAttrValue(node,
                    GoogleMapsFormat.CAMERA_BEARING);
            if (!TextUtils.isEmpty(value)) {
                googleMapsSpec.setCameraBearing(Double.parseDouble(value));
            }

            value = XMLPaserUtil.getAttrValue(node,
                    GoogleMapsFormat.CAMERA_TARGET_LAT);
            if (!TextUtils.isEmpty(value)) {
                googleMapsSpec.setCameraTargetLat(Double.parseDouble(value));
            }

            value = XMLPaserUtil.getAttrValue(node,
                    GoogleMapsFormat.CAMERA_TARGET_LNG);
            if (!TextUtils.isEmpty(value)) {
                googleMapsSpec.setCameraTargetLng(Double.parseDouble(value));
            }

            value = XMLPaserUtil.getAttrValue(node,
                    GoogleMapsFormat.SET_GPS_LOCATION);
            if (!TextUtils.isEmpty(value)) {
                if (value.equalsIgnoreCase("Y")) {
                    googleMapsSpec.setLoadGPSLocation(true);
                }
            }
        }
        return googleMapsSpec;
    }
}
